import axios from '@/libs/api.request'
import { getToken } from '@/libs/util'
import config from '@/config'

class Device {
  constructor() {
    this.$axios = axios;
  }

  /**
   * 根据用户输入的值查询4G执法仪
   * @param param
   * @returns {AxiosPromise}
   */
  /*search4GDevicesByValue = (param) => {
    return axios.request({
      url: '/admin/device/search4GDevicesByValue/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }*/

  /**
   * 查询树型数据
   * @param param
   * @returns {*}
   */
  findDeviceForOrgTree = (param) => {
    let url='/admin/device/find/device/for/org/tree';
/*    if (!!param){
      url=url+"?id=" + param;
    }*/

    return axios.request({
      url: url,
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
      params: param
    })
  }


  /**
   * 分页查询设备
   * @param param
   * @returns {*}
   */
  queryPage = (param) => {
    return axios.request({
      url: '/admin/device/query/page/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }

  /**
   * 按条件查询所有
   * @param param
   * @returns {AxiosPromise}
   */
  queryListWhere = (param) => {
    return axios.request({
      url: '/admin/device/query/list/where',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }

  /**
   * 新增设备
   */
  addDevice = (param) => {
    return axios.request({
      url: '/admin/device/add/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }
  /**
   * 修改设备
   * @param param
   * @returns {*}
   */
  updateDevice = (param) => {
    return axios.request({
      url: '/admin/device/update/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }
  /**
   * 删除设备
   * @param param
   * @returns {*}
   */
  removeDevice = (param) => {
    return axios.request({
      url: '/admin/device/delete/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }


  queryChannelPage = (param) => {
    return axios.request({
      url: '/admin/channel/query/page/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }

  /**
   * 查义起始编号
   * @param param 编号前辍
   * @returns {*}
   */
  getBeginCode = (param) => {
    return axios.request({
      url: '/admin/device/get/begin/code/' + param,
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
    })
  }

  /**
   * 批量注册
   * @param param
   * @returns {*}
   */
  batchAddDevice = (param) => {
    return axios.request({
      url: '/admin/device/batch/save/',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }

  playStart=(deviceId,channelId)=>{
    return this.$axios.request({
      url:'/play/playstart/'+deviceId+'/' + channelId,
      /*url: '/api/play/start/'+deviceId+'/' + channelId,*/
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
    });
  }

  findDeviceByKeyword=(param)=>{
    return axios.request({
      url: '/admin/device/find/device/by/keyword',
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
      params: param
    })
  }

  /**
   * 固件升级
   * @param param
   * @returns {AxiosPromise}
   */
  firmwareUpdgrade=(param)=>{
    return axios.request({
      url: '/admin/device/firmware/updgrade',
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      },
      data: param
    })
  }

  /**
   * 取得国标服务器的参数
   * @param deviceId
   * @returns {*}
   */
  getConfig = (deviceId) => {
    let url='/play/get/config/' + deviceId;
    return axios.request({
      url: url,
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
    })
  }

  /**
   * 开始国标对讲
   * @param deviceId
   * @param app
   * @param stream
   * @returns {*}
   */
  playGbTalk = (deviceId,app,stream) => {
    let url='/play/start/gbtalk/' + deviceId + '/'+app+'/'+stream;
    return axios.request({
      url: url,
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
    })
  }

  /**
   * 停止对讲，给设备发bye命令，通知设备停止发流
   * @param deviceId
   * @param app
   * @param stream
   * @returns {*}
   */
  stopGbTalk = (deviceId,app,stream) => {
    let url='/play/stop/gbtalk/' + deviceId + '/'+app+'/'+stream;
    return axios.request({
      url: url,
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
    })
  }

  getPlayUrl = (urlStr)=>{
    //console.log(">>>> urlStr=%o",urlStr);
    const originUrl = new URL(urlStr);
    //console.log(">>>> originUrl=%o",originUrl);
    let url=null;
    //生产环境
    if (process.env.NODE_ENV == "production") {
      //originUrl.protocol + "//" + originUrl.host + originUrl.pathname + originUrl.search

      url=config.playUrl.pro() +  originUrl.pathname + originUrl.search;
    }else{
      //测试环境
      url=config.playUrl.dev() +  originUrl.pathname + originUrl.search;
    }
    console.log(">>>> 播放url=%o",url);
    return url;
  }

  getPushUrl = (urlStr)=>{
    const originUrl = new URL(urlStr);
    let url=null;
    //生产环境
    if (process.env.NODE_ENV == "production") {
      //originUrl.protocol + "//" + originUrl.host + originUrl.pathname + originUrl.search
      url =  config.playUrl.pro() +  originUrl.pathname + originUrl.search;
    }else{
      //测试环境
      url = config.playUrl.dev() +  originUrl.pathname + originUrl.search;
    }
    console.log(">>>> 推送url=%o",url);
    return url;
  }

  getCurrentPosition =(deviceCode)=>{
    return axios.request({
      url: "admin/device/pos/get/current/"+deviceCode,
      method: 'post',
      dataType: 'json',
      headers: {
        'Content-Type': 'text/html; charset=UTF-8',
        'token': getToken()
      },
    })
  }
}
export default Device;




